package com.hx.er.action.admin.msg;

import org.springframework.beans.factory.annotation.Autowired;

import com.hx.er.action.base.ErAdminAction;
import com.hx.er.bean.detail.MessageDetailBean;
import com.hx.er.common.util.StringUtil;
import com.hx.er.service.admin.IAdminMsgService;

public class AdminMsgReplyAction extends ErAdminAction {
	// 序列化ID
	private static final long serialVersionUID = -1127474451530316813L;

	// 页面消息
	public static final String REPLY_CONTENT_LENGTH_MSG = "admin.msgList.message.reply.length.replyContent";
	
	private long msgId;
	private MessageDetailBean msgDetailBean;
	
	@Autowired
	private IAdminMsgService adminMsgService;

	public String init() {
		this.initPage();
		return SUCCESS;
	}

	public String reply() {
		if (!this.checkParams()) {
			return INPUT;
		}

		this.msgDetailBean.setMsgId(this.msgId);
		this.msgDetailBean.setUpdateUserId(super.getUserInfoLogin().getUserId());
		this.msgDetailBean.setReplyUserId(super.getUserInfoLogin().getUserId());

		int count = this.adminMsgService.replyMsg(this.msgDetailBean);

		if (count == 0) {
			this.initPage();
			this.addActionError(super.getText(OPERATION_FAILURE_MSG));
			return INPUT;
		} else {
			this.addActionMessage(super.getText(OPERATION_SUCCESS_MSG));
			return SUCCESS;
		}
	}

	private void initPage() {
		// 查询消息信息
		this.msgDetailBean = this.adminMsgService.getMsgDetailAliveById(this.msgId);
	}

	private boolean checkParams() {
		// 消息长度证
		if (!StringUtil.checkLength(this.msgDetailBean.getReplyContent(), 0, 512)) {
			this.addFieldError("msg.replyContent", super.getText(REPLY_CONTENT_LENGTH_MSG));
		}

		// 错误信息数量
		int errCount = this.getActionErrors().size() + this.getFieldErrors().size();

		return errCount == 0;
	}

	public long getMsgId() {
		return msgId;
	}

	public void setMsgId(long msgId) {
		this.msgId = msgId;
	}

	public MessageDetailBean getMsgDetailBean() {
		return msgDetailBean;
	}

	public void setMsgDetailBean(MessageDetailBean msgDetailBean) {
		this.msgDetailBean = msgDetailBean;
	}

}
